package org.dtrd.modules.record.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.config.system.SystemState;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.record.entity.bean.WaistDailyRecord;
import org.dtrd.modules.record.entity.bean.WaistLineBean;
import org.dtrd.modules.record.entity.bean.WaistLineChart;
import org.dtrd.modules.record.entity.bean.WaistLineStatistics;
import org.dtrd.modules.record.entity.request.TimePeriodRequest;
import org.dtrd.modules.record.service.IWaistlineRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_SAVE_OR_UPDATE;

/**
 * @Author QR
 * @Date 2021/11/3 4:51 PM
 */
@Api(tags = "腰围打卡")
@RestController
@RequestMapping("/101/09")
public class WaistRecordController extends BaseController {

    @Autowired
    private IWaistlineRecordService waistlineRecordService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("腰围曲线查询")
    @PostMapping("/1010901")
    public Result<?> getWaistLineChart(@RequestBody TimePeriodRequest request) {
        if (request.getPatientId() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        String startDate = null;
        String endDate = null;
        String[] timePeriod = handleTimePeriod(request);
        if (timePeriod != null) {
            startDate = timePeriod[0];
            endDate = timePeriod[1];
        }
        if (startDate == null || endDate == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        int patientId = request.getPatientId();
        WaistLineChart waistLineChart = waistlineRecordService.getWaistLineChart(patientId, startDate, endDate);
        String logContent = String.format("腰围曲线查询，查询患者id：%d", request.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(waistLineChart);
    }

    @ApiOperation("获取腰围统计数据")
    @PostMapping("/1010904")
    public Result<?> getWaistLineStatistics(@RequestBody TimePeriodRequest request) {
        if (request.getPatientId() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        String startDate = null;
        String endDate = null;
        String[] timePeriod = handleTimePeriod(request);
        if (timePeriod != null) {
            startDate = timePeriod[0];
            endDate = timePeriod[1];
        }
        if (startDate == null || endDate == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        int patientId = request.getPatientId();
        WaistLineStatistics waistLineStatistics = waistlineRecordService.getWaistLineStatistics(patientId, startDate, endDate);
        String logContent = String.format("获取腰围统计数据，查询患者id：%d", request.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(waistLineStatistics);
    }

    @ApiOperation("分页查询腰围记录")
    @PostMapping("/1010902")
    public Result<?> getWaistLineList(@RequestBody TimePeriodRequest request){
        if (request.getPatientId() == null) {
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        int type = getIntParam(request.getType(), 1);
        if (type == 1 || type == 3) {
            Integer page = getIntParam(request.getPage(), 1);
            Integer pageSize = getIntParam(request.getPageSize(), 20);
            String startDate = null;
            String endDate = null;
            String[] timePeriod = handleTimePeriod(request);
            if (timePeriod != null) {
                startDate = timePeriod[0];
                endDate = timePeriod[1];
            }
            if (startDate == null || endDate == null) {
                return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
            }
            Integer patientId = request.getPatientId();
            Integer count = waistlineRecordService.getFoodWaistLineDateCount(patientId, startDate, endDate);
            JSONObject result = new JSONObject();
            if (count != null && count > 0) {
                List<WaistDailyRecord> list = waistlineRecordService.getWaistLineRecordByRange(patientId, startDate, endDate, page, pageSize);
                result.put("recordList", list);
                result.put("totalPage", count <= pageSize || pageSize <= 0 ? 1 : count / pageSize + 1);
                result.put("count", count);
            }else {
                result.put("recordList", Collections.EMPTY_LIST);
                result.put("totalPage", 1);
                result.put("count", 0);
            }
            String logContent = String.format("分页查询腰围记录，查询患者id：%d", request.getPatientId());
            baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
            return Result.OK(result);
        }
        return error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }

    @ApiOperation("更新或修改腰围记录")
    @PostMapping("/1010903")
    public Result<?> saveWaistLineRecord(@RequestBody WaistLineBean waistLineBean){
        if (isIntParamInvalid(waistLineBean.getPatientId())
                || StrUtil.isBlank(waistLineBean.getRecordTime())){
            return error(SystemState.ResponseState.STATE_PARAMS_MISSING);
        }
        boolean result = waistlineRecordService.saveWaistLineRecord(waistLineBean);
        String logContent = String.format("更新或修改腰围记录，患者id：%d, 记录时间：%s", waistLineBean.getPatientId(), waistLineBean.getRecordTime());
        baseCommonService.addLog(logContent, OPERATE_TYPE_SAVE_OR_UPDATE);
        return result ? Result.OK() : error(SystemState.ResponseState.STATE_OPERATION_FAILURE);
    }
}
